package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.event.SoundEvent;
	import engine.flag.GameDefine;
	import engine.logic.LogSendLogic;
	import engine.logic.walk.Astar.ForbiddenAreaLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.sound.SoundManage;
	import engine.tip.TipsMainLogic;
	import engine.ui.MainLogic;
	import engine.ui.SystemConfigureUI;
	
	import flash.display.MovieClip;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.text.TextField;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;

	public class RightTopIndexLogic
	{
		private static var _self:	RightTopIndexLogic;
		
		private var _ui		: RightTopIndexUI;
		
		private var _mapTxt	: TextField;
		
		private var musicTurnBtn:MovieClip;
		
		public static function getInstance():RightTopIndexLogic
		{
			if(_self == null)
			{
				_self	=	new RightTopIndexLogic();
			}
			return _self;
		}
		
		public function RightTopIndexLogic()
		{
		}
		
		public function init():void
		{
			_ui		=	RightTopIndexUI.getInstance();
			_ui.mailglow.visible=false;
			_ui.hideglow.visible=false;
			_mapTxt	=	_ui.rightTopPanel.getChildByName( "mapTxt" ) as TextField;
			
			musicTurnBtn	=	_ui.rightTopPanel.getChildByName("mc") as MovieClip;
			musicTurnBtn.buttonMode	=	true;
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			if(userdata.data.musicGroupIndex)
			{
				musicTurnBtn.gotoAndStop(userdata.data.musicGroupIndex+1);
			}
			else
			{
				musicTurnBtn.gotoAndStop(1);
			}
			changeBtnState();
			
			addListener();
		}
		
		public function changeHideGlow(val:Boolean=true):void
		{
			_ui.hideglow.visible=val;
			_ui.hideArrow.visible=val;
			
			if(val)
			{
				_ui.hideglow.play();
				_ui.hideArrow.play();
			}
			else
			{
				_ui.hideglow.stop();
				_ui.hideArrow.stop();
			}
				
		}
		
		private function changeBtnState():void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			if(userdata.data.playGroupIndex)
			{
				var state:int	=	userdata.data.playGroupIndex;
				changeDisplayShow(state==0?true:false);
			}
			else
			{
				changeDisplayShow(true);
			}
		}
		
		private function addListener():void
		{
			musicTurnBtn.addEventListener(MouseEvent.CLICK,onClick);
			_ui.bt1.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt2.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt3.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt4.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt5.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt6.addEventListener( MouseEvent.CLICK, onClick );
//			_ui.bt7.addEventListener( MouseEvent.CLICK, onClick );
//			_ui.bt8.addEventListener( MouseEvent.CLICK, onClick );
//			_ui.bt9.addEventListener( MouseEvent.CLICK, onClick );
			_ui.btn10.addEventListener( MouseEvent.CLICK, onClick );
			_ui.btn11.addEventListener( MouseEvent.CLICK, onClick );
			_ui.btn12.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt13.addEventListener( MouseEvent.CLICK, onClick );
			_ui.bt3.addEventListener( MouseEvent.MOUSE_OVER, onOver );
			_ui.bt3.addEventListener( MouseEvent.MOUSE_OUT, onOut );
			
			_ui.btn11.addEventListener( MouseEvent.MOUSE_OVER, onOver );
			_ui.btn11.addEventListener( MouseEvent.MOUSE_OUT, onOut );
			_ui.bt14.addEventListener( MouseEvent.MOUSE_OVER, onOver );
			_ui.bt14.addEventListener( MouseEvent.MOUSE_OUT, onOut );
			dispatchEvent(musicTurnBtn);
			dispatchEvent(_ui.bt1);
			dispatchEvent(_ui.bt2);
			dispatchEvent(_ui.bt3);
			dispatchEvent(_ui.bt4);
			dispatchEvent(_ui.bt5);
			dispatchEvent(_ui.bt6);
			dispatchEvent(_ui.btn10);
			dispatchEvent(_ui.btn11);
			//
//			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_WORLD_BOSS_TOTAL_PLAYER),showPlayerCount);
			ModelLocator.getInstance().addEventListener(SoundEvent.MUSIC_ON,function(e:SoundEvent):void{musicTurnBtn.gotoAndStop(1)});
			ModelLocator.getInstance().addEventListener(SoundEvent.MUSIC_OFF,function(e:SoundEvent):void{musicTurnBtn.gotoAndStop(2)});
		}
		
		private function dispatchEvent(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			dispatcher.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.MOUSE_OVER));
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.MOUSE_OUT));
		}
		
		private function showPlayerCount( e:CustomMsgEvent ):void
		{
			MessageUI.getInstance().showInfo( e.pList[ 0 ] );
		}
		
		private function onOver( evt : MouseEvent ):void
		{
			if( evt.currentTarget == _ui.btn11 )
			{
				TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIStr_1001635"));
			}else if(evt.currentTarget == _ui.bt3)
			{
				TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIStr_1000731"));
			}
			else if(evt.currentTarget == _ui.bt14)
			{
				TipsMainLogic.instance.isAutoSet	=	true;
				TipsMainLogic.instance.showString(StaticIni.getSwfLangStr(StaticIni.getIniVar("ForbiddenArea_"+GameData.SceneConfig,"tips")));
			}
		}
		
		private function onOut( evt : MouseEvent ):void
		{
			TipsMainLogic.instance.hide();
			TipsMainLogic.instance.isAutoSet	=	false;
		}
		
		private function onClick( evt : MouseEvent ):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			switch( evt.target)
			{
				case musicTurnBtn:
					
					if(SoundManage.getInstance().musicState)
					{
						ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.MUSIC_OFF));
						SystemConfigureUI.getInstance().musicGroup.selectedIndex	=	1;
						musicTurnBtn.gotoAndStop(2);
						userdata.data.musicGroupIndex	=	1;
					}
					else
					{
						ModelLocator.getInstance().dispatchEvent(new SoundEvent(SoundEvent.MUSIC_ON));
						SystemConfigureUI.getInstance().musicGroup.selectedIndex	=	0;
						musicTurnBtn.gotoAndStop(1);
						userdata.data.musicGroupIndex	=	0;
					}
					LogSendLogic.getInstance().btnClickNumSend(22117);
					break;
				case _ui.bt1:
				{
					SystemConfigureUI.getInstance().showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22118);
					break;
				}
				case _ui.bt2:
				{
					_ui.mailglow.stop();
					_ui.mailglow.visible=false;
					MainLogic.isGolw=false;
//					DailyWorkLogic.getInstance().ui.show();
					MailUI.getInstance().showOrHide();
//					MainLogic.getInstance().stopGlow(_ui.bt2);
					LogSendLogic.getInstance().btnClickNumSend(22119);
					break;
				}
				case _ui.bt3://显示
				{
					changeHideGlow(false);
					SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible( true );
					changeDisplayShow(true);
					SystemConfigureUI.getInstance().playGroup.selectedIndex	=	0;
					userdata.data.playGroupIndex	=	0;
					LogSendLogic.getInstance().btnClickNumSend(22120);
					break;
				}
				case _ui.btn11://隐藏
				{
					changeHideGlow(false);
					SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible( false );
					changeDisplayShow(false);
					SystemConfigureUI.getInstance().playGroup.selectedIndex	=	1;
					userdata.data.playGroupIndex	=	1;
					break;
				}
				case _ui.bt4:
				{
					if(WipeOutLogic.getInstance().WipeState)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("97855"));//扫荡中不能打开
						return
					}
					else if(CampBattleLogic.getInstance().isJoinCamp)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("99198"));//阵营战开启中
						return;
					}
					WorldMapLogic.getInstance().ui.showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22121);
					break;
				}
				case _ui.bt5:
				{
					
					break;
				}
				case _ui.bt6:
				{
					if( GameData.SceneType == GameDefine.SceneType_ForbiddenAreaScene )
					{
						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_FORBID_AREA);
						ForbiddenAreaLogic.getInstance().stopSearch();
					}
					else
					{
						SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_FB_LEAVE );
					}
					
					break;
				}
				case _ui.bt7:
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENTRY_CLONE_PLAYER_ROOM)
					
					break;
				}
				case _ui.bt8:
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_CAMP_BATTLE_UI);
					
					break;
				}
				case _ui.bt9:
				{
					_ui.mailglow.visible=false;
					MailUI.getInstance().showOrHide();
					
					break;
				}
				case _ui.btn10:
				{
				//	SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CONVOY_OPEN_UI);
					break;
				}
					
				//查看副本攻略
				case _ui.btn12:
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_ELITE_FB_REPORT_LIST);
					break;
				}
				case _ui.bt13:
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_GUILD_SCENE);
					break ; 
				}
			}
		}
		
		public function changeDisplayShow(state:Boolean):void
		{
			if(state)
			{
				_ui.bt3.visible = _ui.bt3.mouseChildren = _ui.bt3.mouseEnabled = false;
				_ui.btn11.visible = _ui.btn11.mouseChildren = _ui.btn11.mouseEnabled = true;
			}
			else
			{
				_ui.bt3.visible = _ui.bt3.mouseChildren = _ui.bt3.mouseEnabled = true;
				_ui.btn11.visible = _ui.btn11.mouseChildren = _ui.btn11.mouseEnabled = false;
			}
		}
		
		public function changeRightTopIndexStage( sceneConfig : int, sceneType : int ):void
		{
			changeMapName( sceneConfig );
			
			changeOper( sceneType );
			
			changeHideGlow(false);
		}
		
		private function changeMapName( sceneConfig : int ):void
		{
			_mapTxt.htmlText = StaticIni.getSwfLangStr( sceneConfig.toString() );
		}
		
		private function changeOper( sceneType : int ):void
		{
			var oper : Boolean = ( sceneType != GameDefine.SceneType_CS);
			if(sceneType == GameDefine.SceneType_FamilyScene)
			{
				oper =false ; 
			}
			else if(sceneType == GameDefine.SceneType_ForbiddenAreaScene)
			{
				oper = false;
			}
			if(sceneType == GameDefine.SceneType_CS)
			{
				FamilyInfoUI.getInstance().baseBtn.enabled=false;
			}
			else
			{
				FamilyInfoUI.getInstance().baseBtn.enabled=true;
			}
			//用于精英副本显示攻略
			if(GameData.EliteFBScene ==GameDefine.SceneElite_FbScene)
			{
				_ui.bt6.x =892;
				btnOper( _ui.btn12, true );
				btnOper( _ui.bt14, false );
			}
			else if(sceneType == GameDefine.SceneType_ForbiddenAreaScene)
			{
				_ui.bt6.x =892;
				btnOper( _ui.btn12, false );
				btnOper( _ui.bt14, true );
			}
			else
			{
				_ui.bt6.x =858;
				btnOper( _ui.btn12, false );
				btnOper( _ui.bt14, false );
			}
			
			
			if(!oper)
			{
				btnOper( _ui.bt3, oper );
				btnOper( _ui.btn11, oper );
			}
			else
			{
				changeBtnState();
			}
			btnOper( _ui.bt1, oper );
			btnOper( _ui.bt2, oper );
			btnOper( _ui.bt4, oper );
//			
//			btnOper( _ui.bt5, !oper );
			
			if(sceneType == GameDefine.SceneType_FamilyScene&& oper==false)
			{
				btnOper( _ui.bt13, !oper );
				btnOper( _ui.bt6, oper );
			}else
			{
				btnOper( _ui.bt6, !oper );
				btnOper( _ui.bt13, false);
			}
			
//			btnOper( _ui.btn10, !oper );
//			
			if(_ui.bt2.visible==true&&MainLogic.isGolw==true )
			{
				_ui.mailglow.visible=true;
				_ui.mailglow.play();
			}
			else if(_ui.bt2.visible==false || !MainLogic.isGolw)
			{
				_ui.mailglow.visible=false;
			}
		}
		
		private function btnOper( btn : NxButton, oper : Boolean ):void
		{
			btn.mouseChildren = btn.mouseEnabled = btn.visible = oper;
			
		}
	}
}